//
//  JSConfirmOrderController.m
//  jshop
//
//  Created by 张艳江 on 2019/5/24.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCIntegralOrderController.h"
#import "SCSubmitHeadView.h"
#import "SCAddressController.h"
#import "SCIntegralSubmitFootView.h"
#import "SCConfirmGoodsCell.h"
#import "SCMyOrderController.h"

@interface SCIntegralOrderController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel     *priceLab;

@property (strong, nonatomic) SCSubmitHeadView         *headView;
@property (strong, nonatomic) SCIntegralSubmitFootView *footView;
@property (strong, nonatomic) SCModel                  *addressModel;
@property (strong, nonatomic) NSArray                  *giftArray;

@end

@implementation SCIntegralOrderController

- (SCSubmitHeadView *)headView{
    if (!_headView) {
        _headView = [SCSubmitHeadView headerView];
        _headView.frame = CGRectMake(0, 0, kScreenW, 100);
        [_headView.addBtn addTarget:self action:@selector(clickAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}
- (SCIntegralSubmitFootView *)footView{
    if (!_footView) {
        _footView = [SCIntegralSubmitFootView addIntegralSubmitFootView];
        _footView.frame = CGRectMake(0, 0, kScreenW, 137);
    }
    return _footView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"确认订单";
    
    self.tableView.backgroundColor = BGColor;
    self.tableView.rowHeight = 102;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 20)];
    [self.tableView registerNib:[UINib nibWithNibName:@"SCConfirmGoodsCell" bundle:nil] forCellReuseIdentifier:@"SCConfirmGoodsCellID"];
    
    //请求数据
    [self requestGoodsData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.giftArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCConfirmGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCConfirmGoodsCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.giftArray[indexPath.row];
    return cell;
}
#pragma mark - 请求数据
- (void)requestGoodsData{
    
    NSMutableArray *goodsMut = [NSMutableArray array];
    for (SCModel *model in self.goodsArray) {
        NSDictionary *dict;
        if (self.isGiftApp == YES) {
            dict = @{@"gift_id":model.gift_id,
                     @"num":[NSString stringWithFormat:@"%d",model.gift_num],
                     @"cart_id":model.cart_id};
        }else{
            dict = @{@"goods_id":model.goods_id,
                     @"num":[NSString stringWithFormat:@"%d",model.count],
                     @"spec_desc":model.spec,
                     @"cart_id":model.cart_id};
        }
        [goodsMut addObject:[dict mj_JSONString]];
    }
    NSString *gift = [NSString stringWithFormat:@"[%@]",[[goodsMut copy] componentsJoinedByString:@","]];
    
    NSString *url;
    NSString *type;
    if (self.isGiftApp == YES) {
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_order_Submit"];
        type = @"gift";
    }else{
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=Goods_order&a=App_order_Submit"];
        type = @"goods";
    }
    //NSLog(@"-----%@",dict);
    NSDictionary *dict = @{@"token":Token,
                           type:gift};
    
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        [SVProgressHUD dismiss];
        NSLog(@"====%@",result);
        self.addressModel = [SCModel mj_objectWithKeyValues:result[@"data"][@"default"]];
        if (self.addressModel.adress_id != nil) {
            self.headView.model = self.addressModel;
            [self.headView.addBtn setTitle:@"" forState:0];
        }
        self.tableView.tableHeaderView = self.headView;
        
        if (self.isGiftApp == YES) {
            self.footView.integralLab.text = [NSString stringWithFormat:@"%d",[result[@"data"][@"sum"] intValue]];
            self.priceLab.text = [NSString stringWithFormat:@"%d福豆",[result[@"data"][@"sum"] intValue]];
            self.giftArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"gift"]];
        }else{
            self.footView.integralLab.text = [NSString stringWithFormat:@"¥%d",[result[@"data"][@"sum"] intValue]];
            self.footView.fudouLab.text = @"";
            self.priceLab.text = [NSString stringWithFormat:@"¥%d",[result[@"data"][@"sum"] intValue]];
            self.giftArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"goods"]];
        }
        self.tableView.tableFooterView = self.footView;
        [self.tableView reloadData];
    }];
}
#pragma mark - 选择设置地址
- (void)clickAddBtn{
    
    SCAddressController *vc = [[SCAddressController alloc]init];
    vc.isChoose = YES;
    WeakSelf;
    vc.chooseAddress = ^(SCModel *model) {
        weakSelf.addressModel = model;
        weakSelf.headView.model = self.addressModel;
        [weakSelf.headView.addBtn setTitle:@"" forState:0];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 立即支付
- (IBAction)clickGoPay:(id)sender {
    
    if (self.addressModel.adress_id == nil) {
        [SVProgressHUD showImage:nil status:@"请先添加收货地址"];
        return;
    }
    NSMutableArray *goodsMut = [NSMutableArray array];
    for (SCModel *model in self.giftArray) {
        
        NSDictionary *dict;
        if (self.isGiftApp == YES) {
            dict = @{@"gift_id":model.gift_id,
                     @"num":model.num,
                     @"cart_id":model.cart_id};
        }else{
            dict = @{@"goods_id":model.goods_id,
                     @"num":model.num,
                     @"cart_id":model.cart_id,
                     @"spec_desc":model.spec};
        }
        [goodsMut addObject:[dict mj_JSONString]];
    }
   
    NSString *type;
    NSString *typeValue;
    NSString *url;
    if (self.isGiftApp == YES) {
        type = @"gift";
        typeValue = [NSString stringWithFormat:@"{\"gift\":[%@],\"adress_id\":\"%@\"}",[[goodsMut copy] componentsJoinedByString:@","],self.addressModel.adress_id];
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_order_Submits"];
    }else{
        type = @"goods";
        typeValue = [NSString stringWithFormat:@"{\"goods\":[%@],\"adress_id\":\"%@\"}",[[goodsMut copy] componentsJoinedByString:@","],self.addressModel.adress_id];
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=Goods_order&a=App_order_Submits"];
        
    }
    NSDictionary *dict = @{@"token":Token,
                           type:typeValue};
    //NSLog(@"===%@",dict);
    [SVProgressHUD showWithStatus:@"提交中..."];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
        //NSLog(@"返回的数据：%@",result);
        SCMyOrderController *vc = [[SCMyOrderController alloc]init];
        vc.index = 2;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

@end
